package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.entity.UserData;
import com.turing.usdtpay.http.okhttp.model.Progress;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.mvp.contract.LoginContract;
import com.turing.usdtpay.mvp.contract.RobOrderContract;
import com.turing.usdtpay.mvp.presenter.LoginPresenter;
import com.turing.usdtpay.mvp.view.popwindow.UserPopWindow;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.SPHelper;
import com.turing.usdtpay.utils.ToastUtils;

import java.util.ArrayList;

public class LoginActivity extends Activity implements View.OnClickListener ,LoginContract.View {

    private EditText mAccount,mPwd;
    private ImageView mPull;
    private TextView mRetrieve,mRegister;
    private Button mLogin;
    private LoginContract.Present mPresent;
    private ProgressBar mProgress;
    private boolean isExit = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        setPresenter(new LoginPresenter(this));
    }

    private void initView() {
        mAccount = (EditText) findViewById(R.id.et_account);
        mPwd = (EditText) findViewById(R.id.et_pwd);
        mPull = (ImageView) findViewById(R.id.iv_pull);
        mRetrieve = (TextView) findViewById(R.id.tv_retrieve);
        mRegister = (TextView) findViewById(R.id.tv_register);
        mLogin = (Button) findViewById(R.id.btn_login);
        mProgress = (ProgressBar) findViewById(R.id.progress);
        mPull.setOnClickListener(this);
        mRegister.setOnClickListener(this);
        mRetrieve.setOnClickListener(this);
        mLogin.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        if (v.getId() == mPull.getId()){
            ArrayList<UserData> userList = UserManager.getInstance().getUserList();
            final UserPopWindow popWindow = new UserPopWindow(this,R.layout.popwindow_user_list,mAccount.getWidth(),200,userList);
            popWindow.setItemOnClickListener(new UserPopWindow.OnClickListener() {
                @Override
                public void onItemClick(String account, String pwd) {
                    mAccount.setText(account);
                    mPwd.setText(pwd);
                    popWindow.dismiss();
                }
            });
            popWindow.showAsDropDown(mAccount);
        }else if (v.getId() == mRetrieve.getId()){
            startActivity(new Intent(this,RetrievePwdActivity.class));
        }else if (v.getId() == mLogin.getId()){
            if (FormatUtils.isEmail(mAccount.getText().toString())){
                if (TextUtils.isEmpty(mPwd.getText().toString())){
                    ToastUtils.showToast(this,"请输入密码");
                    return;
                }
                mProgress.setVisibility(View.VISIBLE);
                mPresent.login(mAccount.getText().toString(),mPwd.getText().toString());
            }else {
                ToastUtils.showToast(this,"请输入邮箱账号");
            }
        }else if (v.getId() == mRegister.getId()){
            startActivityForResult(new Intent(this,RegisterActivity.class),1);
        }
    }

    @Override
    public void loginSuccess() {
        mProgress.setVisibility(View.GONE);
        startActivity(new Intent(this,RodOrderActivity.class));
        finish();
    }

    @Override
    public void loginFail(String msg) {
        mProgress.setVisibility(View.GONE);
        ToastUtils.showToast(this,msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 3){
            if (data != null){
                String account = data.getStringExtra("account");
                String pwd = data.getStringExtra("pwd");
                mProgress.setVisibility(View.VISIBLE);
                mPresent.login(account,pwd);
            }
        }
    }

    @Override
    public void setPresenter(LoginContract.Present presenter) {
        mPresent = presenter;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.d("onKeyDown keyCode ->"+keyCode+"event ->"+event);
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (isExit){
                finish();
            }else {
                isExit = true;
                showLongToast("再点击一次退出");
            }
        }
        return false;
    }


    protected void showLongToast(CharSequence text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}
